Artificial Intelligence Nanodegree

Computer Vision Capstone

Project: Facial Keypoint Detection


Welcome to the final Computer Vision project in the Artificial Intelligence Nanodegree program!

In this project, you’ll combine your knowledge of computer vision techniques and deep learning to build and end-to-end facial keypoint recognition system! Facial keypoints include points around the eyes, nose, and mouth on any face and are used in many applications, from facial tracking to emotion recognition.

There are three main parts to this project:

Part 1 : Investigating OpenCV, pre-processing, and face detection

Part 2 : Training a Convolutional Neural Network (CNN) to detect facial keypoints

Part 3 : Putting parts 1 and 2 together to identify facial keypoints on any image!


*Here's what you need to know to complete the project:

  1. In this notebook, some template code has already been provided for you, and you will need to implement additional functionality to successfully complete this project. You will not need to modify the included code beyond what is requested.

    a. Sections that begin with '(IMPLEMENTATION)' in the header indicate that the following block of code will require additional functionality which you must provide. Instructions will be provided for each section, and the specifics of the implementation are marked in the code block with a 'TODO' statement. Please be sure to read the instructions carefully!

  1. In addition to implementing code, there will be questions that you must answer which relate to the project and your implementation.

    a. Each section where you will answer a question is preceded by a 'Question X' header.

    b. Carefully read each question and provide thorough answers in the following text boxes that begin with 'Answer:'.

Note: Code and Markdown cells can be executed using the Shift + Enter keyboard shortcut. Markdown cells can be edited by double-clicking the cell to enter edit mode.

The rubric contains optional suggestions for enhancing the project beyond the minimum requirements. If you decide to pursue the "(Optional)" sections, you should include the code in this IPython notebook.

Your project submission will be evaluated based on your answers to each of the questions and the code implementations you provide.

Steps to Complete the Project

Each part of the notebook is further broken down into separate steps. Feel free to use the links below to navigate the notebook.

In this project you will get to explore a few of the many computer vision algorithms built into the OpenCV library. This expansive computer vision library is now almost 20 years old and still growing!

The project itself is broken down into three large parts, then even further into separate steps. Make sure to read through each step, and complete any sections that begin with '(IMPLEMENTATION)' in the header; these implementation sections may contain multiple TODOs that will be marked in code. For convenience, we provide links to each of these steps below.

Part 1 : Investigating OpenCV, pre-processing, and face detection

  • Step 0: Detect Faces Using a Haar Cascade Classifier
  • Step 1: Add Eye Detection
  • Step 2: De-noise an Image for Better Face Detection
  • Step 3: Blur an Image and Perform Edge Detection
  • Step 4: Automatically Hide the Identity of an Individual

Part 2 : Training a Convolutional Neural Network (CNN) to detect facial keypoints

  • Step 5: Create a CNN to Recognize Facial Keypoints
  • Step 6: Compile and Train the Model
  • Step 7: Visualize the Loss and Answer Questions

Part 3 : Putting parts 1 and 2 together to identify facial keypoints on any image!

  • Step 8: Build a Robust Facial Keypoints Detector (Complete the CV Pipeline)

Step 0: Detect Faces Using a Haar Cascade Classifier

Have you ever wondered how Facebook automatically tags images with your friends' faces? Or how high-end cameras automatically find and focus on a certain person's face? Applications like these depend heavily on the machine learning task known as face detection - which is the task of automatically finding faces in images containing people.

At its root face detection is a classification problem - that is a problem of distinguishing between distinct classes of things. With face detection these distinct classes are 1) images of human faces and 2) everything else.

We use OpenCV's implementation of Haar feature-based cascade classifiers to detect human faces in images. OpenCV provides many pre-trained face detectors, stored as XML files on github. We have downloaded one of these detectors and stored it in the detector_architectures directory.

Import Resources

In the next python cell, we load in the required libraries for this section of the project.

In [1]:
# Import required libraries for this section

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import math
import cv2                     # OpenCV library for computer vision
from PIL import Image
import time 

Next, we load in and display a test image for performing face detection.

Note: by default OpenCV assumes the ordering of our image's color channels are Blue, then Green, then Red. This is slightly out of order with most image types we'll use in these experiments, whose color channels are ordered Red, then Green, then Blue. In order to switch the Blue and Red channels of our test image around we will use OpenCV's cvtColor function, which you can read more about by checking out some of its documentation located here. This is a general utility function that can do other transformations too like converting a color image to grayscale, and transforming a standard color image to HSV color space.

In [2]:
# Load in color image for face detection
image = cv2.imread('images/test_image_1.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Plot our image using subplots to specify a size and title
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Original Image')
ax1.imshow(image)
Out[2]:
<matplotlib.image.AxesImage at 0x7f4ca034a5f8>

There are a lot of people - and faces - in this picture. 13 faces to be exact! In the next code cell, we demonstrate how to use a Haar Cascade classifier to detect all the faces in this test image.

This face detector uses information about patterns of intensity in an image to reliably detect faces under varying light conditions. So, to use this face detector, we'll first convert the image from color to grayscale.

Then, we load in the fully trained architecture of the face detector -- found in the file haarcascade_frontalface_default.xml - and use it on our image to find faces!

To learn more about the parameters of the detector see this post.

In [3]:
# Convert the RGB  image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray, 4, 6)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face Detections')
ax1.imshow(image_with_detections)
Number of faces detected: 13
Out[3]:
<matplotlib.image.AxesImage at 0x7f4ca027abe0>

In the above code, faces is a numpy array of detected faces, where each row corresponds to a detected face. Each detected face is a 1D array with four entries that specifies the bounding box of the detected face. The first two entries in the array (extracted in the above code as x and y) specify the horizontal and vertical positions of the top left corner of the bounding box. The last two entries in the array (extracted here as w and h) specify the width and height of the box.


Step 1: Add Eye Detections

There are other pre-trained detectors available that use a Haar Cascade Classifier - including full human body detectors, license plate detectors, and more. A full list of the pre-trained architectures can be found here.

To test your eye detector, we'll first read in a new test image with just a single face.

In [4]:
# Load in color image for face detection
image = cv2.imread('images/james.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Plot the RGB image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Original Image')
ax1.imshow(image)
Out[4]:
<matplotlib.image.AxesImage at 0x7f4ca01f3748>

Notice that even though the image is a black and white image, we have read it in as a color image and so it will still need to be converted to grayscale in order to perform the most accurate face detection.

So, the next steps will be to convert this image to grayscale, then load OpenCV's face detector and run it with parameters that detect this face accurately.

In [5]:
# Convert the RGB  image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray, 1.25, 6)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face Detection')
ax1.imshow(image_with_detections)
Number of faces detected: 1
Out[5]:
<matplotlib.image.AxesImage at 0x7f4c66b73240>

(IMPLEMENTATION) Add an eye detector to the current face detection setup.

A Haar-cascade eye detector can be included in the same way that the face detector was and, in this first task, it will be your job to do just this.

To set up an eye detector, use the stored parameters of the eye cascade detector, called haarcascade_eye.xml, located in the detector_architectures subdirectory. In the next code cell, create your eye detector and store its detections.

A few notes before you get started:

First, make sure to give your loaded eye detector the variable name

eye_cascade

and give the list of eye regions you detect the variable name

eyes

Second, since we've already run the face detector over this image, you should only search for eyes within the rectangular face regions detected in faces. This will minimize false detections.

Lastly, once you've run your eye detector over the facial detection region, you should display the RGB image with both the face detection boxes (in red) and your eye detections (in green) to verify that everything works as expected.

In [6]:
# Make a copy of the original image to plot rectangle detections
image_with_detections = np.copy(image)   

# Loop over the detections and draw their corresponding face detection boxes
for (x,y,w,h) in faces:
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h),(255,0,0), 3)  
    
# Do not change the code above this comment!

    
## TODO: Add eye detection, using haarcascade_eye.xml, to the current face detector algorithm
## TODO: Loop over the eye detections and draw their corresponding boxes in green on image_with_detections
eye_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_eye.xml')


for (x,y,w,h) in faces:
    face_gray = gray[y:y+h, x:x+w]
    face_image_with_detections = image_with_detections[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(face_gray)
    for (x,y,w,h) in eyes:
        cv2.rectangle(face_image_with_detections, (x,y), (x+w,y+h), (0,255,0), 2)
    
# Plot the image with both faces and eyes detected
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face and Eye Detection')
ax1.imshow(image_with_detections)
Out[6]:
<matplotlib.image.AxesImage at 0x7f4c66ae85f8>

(Optional) Add face and eye detection to your laptop camera

It's time to kick it up a notch, and add face and eye detection to your laptop's camera! Afterwards, you'll be able to show off your creation like in the gif shown below - made with a completed version of the code!

Notice that not all of the detections here are perfect - and your result need not be perfect either. You should spend a small amount of time tuning the parameters of your detectors to get reasonable results, but don't hold out for perfection. If we wanted perfection we'd need to spend a ton of time tuning the parameters of each detector, cleaning up the input image frames, etc. You can think of this as more of a rapid prototype.

The next cell contains code for a wrapper function called laptop_camera_face_eye_detector that, when called, will activate your laptop's camera. You will place the relevant face and eye detection code in this wrapper function to implement face/eye detection and mark those detections on each image frame that your camera captures.

Before adding anything to the function, you can run it to get an idea of how it works - a small window should pop up showing you the live feed from your camera; you can press any key to close this window.

Note: Mac users may find that activating this function kills the kernel of their notebook every once in a while. If this happens to you, just restart your notebook's kernel, activate cell(s) containing any crucial import statements, and you'll be good to go!

In [7]:
### Add face and eye detection to this laptop camera function 
# Make sure to draw out all faces/eyes found in each frame on the shown video feed

import cv2
import time 

face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_eye.xml')


def extract_features(frame):
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    output = np.copy(frame)
    
    for (x,y,w,h) in faces:
        face_gray = gray[y:y+h, x:x+w]
        face_output = output[y:y+h, x:x+w]
        for (x,y,w,h) in eye_cascade.detectMultiScale(face_gray):
            cv2.rectangle(face_output, (x,y), (x+w,y+h), (0,255,0), 2)
            
    return output

# wrapper function for face/eye detection with your laptop camera
def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep the video stream open
    while rval:
        # Plot the image from camera with all the face and eye detections marked
        cv2.imshow("face detection activated", extract_features(frame))
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # Exit by pressing any key
            # Destroy windows 
            cv2.destroyAllWindows()
            
            # Make sure window closes on OSx
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
In [8]:
# Call the laptop camera face/eye detector function above
laptop_camera_go()
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-8-0144377e03a2> in <module>()
      1 # Call the laptop camera face/eye detector function above
----> 2 laptop_camera_go()

<ipython-input-7-d5f9c09b43ae> in laptop_camera_go()
     24 def laptop_camera_go():
     25     # Create instance of video capturer
---> 26     cv2.namedWindow("face detection activated")
     27     vc = cv2.VideoCapture(0)
     28 

error: /io/opencv/modules/highgui/src/window.cpp:565: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow

Step 2: De-noise an Image for Better Face Detection

Image quality is an important aspect of any computer vision task. Typically, when creating a set of images to train a deep learning network, significant care is taken to ensure that training images are free of visual noise or artifacts that hinder object detection. While computer vision algorithms - like a face detector - are typically trained on 'nice' data such as this, new test data doesn't always look so nice!

When applying a trained computer vision algorithm to a new piece of test data one often cleans it up first before feeding it in. This sort of cleaning - referred to as pre-processing - can include a number of cleaning phases like blurring, de-noising, color transformations, etc., and many of these tasks can be accomplished using OpenCV.

In this short subsection we explore OpenCV's noise-removal functionality to see how we can clean up a noisy image, which we then feed into our trained face detector.

Create a noisy image to work with

In the next cell, we create an artificial noisy version of the previous multi-face image. This is a little exaggerated - we don't typically get images that are this noisy - but image noise, or 'grainy-ness' in a digitial image - is a fairly common phenomenon.

In [8]:
def display_image(image, title=''):
    fig = plt.figure(figsize = (8,8))
    ax1 = fig.add_subplot(111)
    ax1.set_xticks([])
    ax1.set_yticks([])

    ax1.set_title(title)
    ax1.imshow(image)
    
In [9]:
# Load in the multi-face test image again
image = cv2.imread('images/test_image_1.jpg')

# Convert the image copy to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Make an array copy of this image
image_with_noise = np.asarray(image)

# Create noise - here we add noise sampled randomly from a Gaussian distribution: a common model for noise
noise_level = 40
noise = np.random.randn(image.shape[0],image.shape[1],image.shape[2])*noise_level

# Add this noise to the array image copy
image_with_noise = image_with_noise + noise

# Convert back to uint8 format
image_with_noise = np.asarray([np.uint8(np.clip(i,0,255)) for i in image_with_noise])

# Plot our noisy image!
display_image(image_with_noise, 'Noisy Image')

In the context of face detection, the problem with an image like this is that - due to noise - we may miss some faces or get false detections.

In the next cell we apply the same trained OpenCV detector with the same settings as before, to see what sort of detections we get.

In [10]:
def detect_faces(image, title):
    # Convert the RGB  image to grayscale
    gray_noise = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

    # Extract the pre-trained face detector from an xml file
    face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

    # Detect the faces in image
    faces = face_cascade.detectMultiScale(gray_noise, 4, 6)

    # Print the number of faces detected in the image
    print('Number of faces detected:', len(faces))

    # Make a copy of the orginal image to draw face detections on
    image_with_detections = np.copy(image)

    # Get the bounding box for each detected face
    for (x,y,w,h) in faces:
        # Add a red bounding box to the detections image
        cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)


    # Display the image with the detections
    display_image(image_with_detections, title)
    
detect_faces(image_with_noise, 'Noisy Image with Face Detections')
Number of faces detected: 12

With this added noise we now miss one of the faces!

(IMPLEMENTATION) De-noise this image for better face detection

Time to get your hands dirty: using OpenCV's built in color image de-noising functionality called fastNlMeansDenoisingColored - de-noise this image enough so that all the faces in the image are properly detected. Once you have cleaned the image in the next cell, use the cell that follows to run our trained face detector over the cleaned image to check out its detections.

You can find its official documentation here and a useful example here.

Note: you can keep all parameters except photo_render fixed as shown in the second link above. Play around with the value of this parameter - see how it affects the resulting cleaned image.

In [14]:
## TODO: Use OpenCV's built in color image de-noising function to clean up our noisy image!
denoised_image = cv2.fastNlMeansDenoisingColored(image_with_noise,None,15,30,7,21)# your final de-noised image (should be RGB)
display_image(denoised_image, "Denoised image")
In [15]:
## TODO: Run the face detector on the de-noised image to improve your detections and display the result
detect_faces(denoised_image, "Denoised image with Face Detection")
Number of faces detected: 13

Step 3: Blur an Image and Perform Edge Detection

Now that we have developed a simple pipeline for detecting faces using OpenCV - let's start playing around with a few fun things we can do with all those detected faces!

Importance of Blur in Edge Detection

Edge detection is a concept that pops up almost everywhere in computer vision applications, as edge-based features (as well as features built on top of edges) are often some of the best features for e.g., object detection and recognition problems.

Edge detection is a dimension reduction technique - by keeping only the edges of an image we get to throw away a lot of non-discriminating information. And typically the most useful kind of edge-detection is one that preserves only the important, global structures (ignoring local structures that aren't very discriminative). So removing local structures / retaining global structures is a crucial pre-processing step to performing edge detection in an image, and blurring can do just that.

Below is an animated gif showing the result of an edge-detected cat taken from Wikipedia, where the image is gradually blurred more and more prior to edge detection. When the animation begins you can't quite make out what it's a picture of, but as the animation evolves and local structures are removed via blurring the cat becomes visible in the edge-detected image.

Edge detection is a convolution performed on the image itself, and you can read about Canny edge detection on this OpenCV documentation page.

Canny edge detection

In the cell below we load in a test image, then apply Canny edge detection on it. The original image is shown on the left panel of the figure, while the edge-detected version of the image is shown on the right. Notice how the result looks very busy - there are too many little details preserved in the image before it is sent to the edge detector. When applied in computer vision applications, edge detection should preserve global structure; doing away with local structures that don't help describe what objects are in the image.

In [16]:
def display_ab(original, other, title):
    fig = plt.figure(figsize = (15,15))
    ax1 = fig.add_subplot(121)
    ax1.set_xticks([])
    ax1.set_yticks([])

    ax1.set_title('Original Image')
    ax1.imshow(original)

    ax2 = fig.add_subplot(122)
    ax2.set_xticks([])
    ax2.set_yticks([])

    ax2.set_title(title)
    ax2.imshow(other, cmap='gray')    

# Load in the image
image = cv2.imread('images/fawzia.jpg')

# Convert to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
def apply_canny(source):
    # Convert to grayscale
    gray = cv2.cvtColor(source, cv2.COLOR_RGB2GRAY)  

    # Perform Canny edge detection
    edges = cv2.Canny(gray,100,200)

    # Dilate the image to amplify edges
    return cv2.dilate(edges, None)

# Plot the RGB and edge-detected image
display_ab(image, apply_canny(image), "Canny Edges")

Without first blurring the image, and removing small, local structures, a lot of irrelevant edge content gets picked up and amplified by the detector (as shown in the right panel above).

(IMPLEMENTATION) Blur the image then perform edge detection

In the next cell, you will repeat this experiment - blurring the image first to remove these local structures, so that only the important boudnary details remain in the edge-detected image.

Blur the image by using OpenCV's filter2d functionality - which is discussed in this documentation page - and use an averaging kernel of width equal to 4.

In [17]:
### TODO: Blur the test imageusing OpenCV's filter2d functionality, 
# Use an averaging kernel, and a kernel width equal to 4

kernel = np.ones((4,4),np.float32)/16
blurred = cv2.filter2D(image,-1,kernel)

## TODO: Then perform Canny edge detection and display the output
display_ab(image, apply_canny(blurred), "Blurred Canny Edges")

Step 4: Automatically Hide the Identity of an Individual

If you film something like a documentary or reality TV, you must get permission from every individual shown on film before you can show their face, otherwise you need to blur it out - by blurring the face a lot (so much so that even the global structures are obscured)! This is also true for projects like Google's StreetView maps - an enormous collection of mapping images taken from a fleet of Google vehicles. Because it would be impossible for Google to get the permission of every single person accidentally captured in one of these images they blur out everyone's faces, the detected images must automatically blur the identity of detected people. Here's a few examples of folks caught in the camera of a Google street view vehicle.

Read in an image to perform identity detection

Let's try this out for ourselves. Use the face detection pipeline built above and what you know about using the filter2D to blur and image, and use these in tandem to hide the identity of the person in the following image - loaded in and printed in the next cell.

In [18]:
# Load in the image
image = cv2.imread('images/gus.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Display the image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('Original Image')
ax1.imshow(image)
Out[18]:
<matplotlib.image.AxesImage at 0x7f4c5c349dd8>

(IMPLEMENTATION) Use blurring to hide the identity of an individual in an image

The idea here is to 1) automatically detect the face in this image, and then 2) blur it out! Make sure to adjust the parameters of the averaging blur filter to completely obscure this person's identity.

In [19]:
## TODO: Implement face detection
def blur_faces(source):
    kernel = np.ones((64,64),np.float32)/4096
    result = np.copy(source)  
    for (x,y,w,h) in face_cascade.detectMultiScale(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)):
        blurred = result[y:y+h, x:x+w]
        blurred = cv2.filter2D(blurred,-1,kernel, dst=blurred)

    return result
        
## TODO: Blur the bounding box around each detected face using an averaging filter and display the result
display_ab(image, blur_faces(image), "Blurred face")

(Optional) Build identity protection into your laptop camera

In this optional task you can add identity protection to your laptop camera, using the previously completed code where you added face detection to your laptop camera - and the task above. You should be able to get reasonable results with little parameter tuning - like the one shown in the gif below.

As with the previous video task, to make this perfect would require significant effort - so don't strive for perfection here, strive for reasonable quality.

The next cell contains code a wrapper function called laptop_camera_identity_hider that - when called - will activate your laptop's camera. You need to place the relevant face detection and blurring code developed above in this function in order to blur faces entering your laptop camera's field of view.

Before adding anything to the function you can call it to get a hang of how it works - a small window will pop up showing you the live feed from your camera, you can press any key to close this window.

Note: Mac users may find that activating this function kills the kernel of their notebook every once in a while. If this happens to you, just restart your notebook's kernel, activate cell(s) containing any crucial import statements, and you'll be good to go!

In [20]:
### Insert face detection and blurring code into the wrapper below to create an identity protector on your laptop!
import cv2
import time 

def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep video stream open
    while rval:
        # Plot image from camera with detections marked
        cv2.imshow("face detection activated", blur_faces(frame))
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # Exit by pressing any key
            # Destroy windows
            cv2.destroyAllWindows()
            
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
        
In [21]:
# Run laptop identity hider
laptop_camera_go()
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-21-6687f8301aa7> in <module>()
      1 # Run laptop identity hider
----> 2 laptop_camera_go()

<ipython-input-20-0701916aaeb8> in laptop_camera_go()
      5 def laptop_camera_go():
      6     # Create instance of video capturer
----> 7     cv2.namedWindow("face detection activated")
      8     vc = cv2.VideoCapture(0)
      9 

error: /io/opencv/modules/highgui/src/window.cpp:565: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow

Step 5: Create a CNN to Recognize Facial Keypoints

OpenCV is often used in practice with other machine learning and deep learning libraries to produce interesting results. In this stage of the project you will create your own end-to-end pipeline - employing convolutional networks in keras along with OpenCV - to apply a "selfie" filter to streaming video and images.

You will start by creating and then training a convolutional network that can detect facial keypoints in a small dataset of cropped images of human faces. We then guide you towards OpenCV to expanding your detection algorithm to more general images. What are facial keypoints? Let's take a look at some examples.

Facial keypoints (also called facial landmarks) are the small blue-green dots shown on each of the faces in the image above - there are 15 keypoints marked in each image. They mark important areas of the face - the eyes, corners of the mouth, the nose, etc. Facial keypoints can be used in a variety of machine learning applications from face and emotion recognition to commercial applications like the image filters popularized by Snapchat.

Below we illustrate a filter that, using the results of this section, automatically places sunglasses on people in images (using the facial keypoints to place the glasses correctly on each face). Here, the facial keypoints have been colored lime green for visualization purposes.

Make a facial keypoint detector

But first things first: how can we make a facial keypoint detector? Well, at a high level, notice that facial keypoint detection is a regression problem. A single face corresponds to a set of 15 facial keypoints (a set of 15 corresponding $(x, y)$ coordinates, i.e., an output point). Because our input data are images, we can employ a convolutional neural network to recognize patterns in our images and learn how to identify these keypoint given sets of labeled data.

In order to train a regressor, we need a training set - a set of facial image / facial keypoint pairs to train on. For this we will be using this dataset from Kaggle. We've already downloaded this data and placed it in the data directory. Make sure that you have both the training and test data files. The training dataset contains several thousand $96 \times 96$ grayscale images of cropped human faces, along with each face's 15 corresponding facial keypoints (also called landmarks) that have been placed by hand, and recorded in $(x, y)$ coordinates. This wonderful resource also has a substantial testing set, which we will use in tinkering with our convolutional network.

To load in this data, run the Python cell below - notice we will load in both the training and testing sets.

The load_data function is in the included utils.py file.

In [22]:
from utils import *

# Load training set
X_train, y_train = load_data()
print("X_train.shape == {}".format(X_train.shape))
print("y_train.shape == {}; y_train.min == {:.3f}; y_train.max == {:.3f}".format(
    y_train.shape, y_train.min(), y_train.max()))

# Load testing set
X_test, _ = load_data(test=True)
print("X_test.shape == {}".format(X_test.shape))
Using TensorFlow backend.
X_train.shape == (2140, 96, 96, 1)
y_train.shape == (2140, 30); y_train.min == -0.920; y_train.max == 0.996
X_test.shape == (1783, 96, 96, 1)

The load_data function in utils.py originates from this excellent blog post, which you are strongly encouraged to read. Please take the time now to review this function. Note how the output values - that is, the coordinates of each set of facial landmarks - have been normalized to take on values in the range $[-1, 1]$, while the pixel values of each input point (a facial image) have been normalized to the range $[0,1]$.

Note: the original Kaggle dataset contains some images with several missing keypoints. For simplicity, the load_data function removes those images with missing labels from the dataset. As an optional extension, you are welcome to amend the load_data function to include the incomplete data points.

Visualize the Training Data

Execute the code cell below to visualize a subset of the training data.

In [23]:
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure(figsize=(20,20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1, xticks=[], yticks=[])
    plot_data(X_train[i], y_train[i], ax)

For each training image, there are two landmarks per eyebrow (four total), three per eye (six total), four for the mouth, and one for the tip of the nose.

Review the plot_data function in utils.py to understand how the 30-dimensional training labels in y_train are mapped to facial locations, as this function will prove useful for your pipeline.

(IMPLEMENTATION) Specify the CNN Architecture

In this section, you will specify a neural network for predicting the locations of facial keypoints. Use the code cell below to specify the architecture of your neural network. We have imported some layers that you may find useful for this task, but if you need to use more Keras layers, feel free to import them in the cell.

Your network should accept a $96 \times 96$ grayscale image as input, and it should output a vector with 30 entries, corresponding to the predicted (horizontal and vertical) locations of 15 facial keypoints. If you are not sure where to start, you can find some useful starting architectures in this blog, but you are not permitted to copy any of the architectures that you find online.

In [24]:
# Import deep learning resources from Keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Activation
from keras.layers import Flatten, Dense, BatchNormalization


## TODO: Specify a CNN architecture
# Your model should accept 96x96 pixel graysale images in
# It should have a fully-connected output layer with 30 values (2 for each facial keypoint)

def build_model(verbose=False, normalisation=True):
    
    def add_conv(model, filters, **kwargs):
        model.add(Conv2D(filters=filters, kernel_size=3, strides=2, padding='same', **kwargs))
        if normalisation:
            model.add(BatchNormalization())
        model.add(Activation('relu'))
    
    model = Sequential()
    add_conv(model, filters=32, input_shape=(96, 96, 1))
    add_conv(model, filters=64)
    add_conv(model, filters=128)
    add_conv(model, filters=256)
    add_conv(model, filters=512)
    add_conv(model, filters=1024)
    model.add(Flatten())
    model.add(Dense(30, activation='tanh'))

    # Summarize the model
    if verbose:
        model.summary()
    
    return model

build_model(verbose=True)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 48, 48, 32)        320       
_________________________________________________________________
batch_normalization_1 (Batch (None, 48, 48, 32)        128       
_________________________________________________________________
activation_1 (Activation)    (None, 48, 48, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
batch_normalization_2 (Batch (None, 24, 24, 64)        256       
_________________________________________________________________
activation_2 (Activation)    (None, 24, 24, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 12, 12, 128)       73856     
_________________________________________________________________
batch_normalization_3 (Batch (None, 12, 12, 128)       512       
_________________________________________________________________
activation_3 (Activation)    (None, 12, 12, 128)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 6, 6, 256)         295168    
_________________________________________________________________
batch_normalization_4 (Batch (None, 6, 6, 256)         1024      
_________________________________________________________________
activation_4 (Activation)    (None, 6, 6, 256)         0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 3, 3, 512)         1180160   
_________________________________________________________________
batch_normalization_5 (Batch (None, 3, 3, 512)         2048      
_________________________________________________________________
activation_5 (Activation)    (None, 3, 3, 512)         0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 2, 2, 1024)        4719616   
_________________________________________________________________
batch_normalization_6 (Batch (None, 2, 2, 1024)        4096      
_________________________________________________________________
activation_6 (Activation)    (None, 2, 2, 1024)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 4096)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 30)                122910    
=================================================================
Total params: 6,418,590
Trainable params: 6,414,558
Non-trainable params: 4,032
_________________________________________________________________
Out[24]:
<keras.models.Sequential at 0x7f4bc1fd66d8>

Step 6: Compile and Train the Model

After specifying your architecture, you'll need to compile and train the model to detect facial keypoints'

(IMPLEMENTATION) Compile and Train the Model

Use the compile method to configure the learning process. Experiment with your choice of optimizer; you may have some ideas about which will work best (SGD vs. RMSprop, etc), but take the time to empirically verify your theories.

Use the fit method to train the model. Break off a validation set by setting validation_split=0.2. Save the returned History object in the history variable.

Your model is required to attain a validation loss (measured as mean squared error) of at least XYZ. When you have finished training, save your model as an HDF5 file with file path my_model.h5.

In [4]:
from keras.optimizers import SGD, RMSprop, Adagrad, Adadelta, Adam, Adamax, Nadam

hists = {
    "normalised": {},
    "unnormalised": {}
}
models = {
    "normalised": {},
    "unnormalised": {}
}

nstring = {
    True: "normalised",
    False: "unnormalised"
}

for optimizer in ['sgd', 'rmsprop', 'adagrad', 'adadelta', 'adam', 'adamax', 'nadam']:
    for normalised in [True, False]:
        model = build_model(normalisation=normalised)

        print(f" *** training with {optimizer} optimiser ({nstring[normalised]})***")

        ## TODO: Compile the model
        model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy'])

        ## TODO: Train the model
        hists[nstring[normalised]][optimizer] = model.fit(X_train, y_train, epochs=30, validation_split=0.2)

        ## TODO: Save the model as model.h5
        model.save(f'my_model.{optimizer}.{nstring[normalised]}.h5')

        models[nstring[normalised]][optimizer] = model
 *** training with sgd optimiser (normalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 3s - loss: 0.1937 - acc: 0.2477 - val_loss: 0.1130 - val_acc: 0.0327
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.1418 - acc: 0.3394 - val_loss: 0.0790 - val_acc: 0.0327
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.1139 - acc: 0.3581 - val_loss: 0.0555 - val_acc: 0.0467
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0940 - acc: 0.3703 - val_loss: 0.0439 - val_acc: 0.0888
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0814 - acc: 0.3867 - val_loss: 0.0374 - val_acc: 0.2593
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0679 - acc: 0.3949 - val_loss: 0.0361 - val_acc: 0.3925
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0607 - acc: 0.3919 - val_loss: 0.0394 - val_acc: 0.3621
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0547 - acc: 0.3914 - val_loss: 0.0414 - val_acc: 0.4065
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0482 - acc: 0.3984 - val_loss: 0.0448 - val_acc: 0.3995
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0426 - acc: 0.4071 - val_loss: 0.0504 - val_acc: 0.4182
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0403 - acc: 0.4235 - val_loss: 0.0525 - val_acc: 0.4065
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0364 - acc: 0.4276 - val_loss: 0.0569 - val_acc: 0.3879
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0344 - acc: 0.4363 - val_loss: 0.0623 - val_acc: 0.4393
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0318 - acc: 0.4393 - val_loss: 0.0632 - val_acc: 0.3949
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0294 - acc: 0.4393 - val_loss: 0.0652 - val_acc: 0.4089
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0276 - acc: 0.4533 - val_loss: 0.0621 - val_acc: 0.4369
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0251 - acc: 0.4579 - val_loss: 0.0636 - val_acc: 0.4486
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0255 - acc: 0.4591 - val_loss: 0.0613 - val_acc: 0.4603
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0230 - acc: 0.4468 - val_loss: 0.0578 - val_acc: 0.4579
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0216 - acc: 0.4813 - val_loss: 0.0560 - val_acc: 0.4416
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0216 - acc: 0.4842 - val_loss: 0.0547 - val_acc: 0.4603
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0215 - acc: 0.4977 - val_loss: 0.0532 - val_acc: 0.4533
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0193 - acc: 0.4796 - val_loss: 0.0532 - val_acc: 0.4509
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0188 - acc: 0.4977 - val_loss: 0.0509 - val_acc: 0.4579
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0180 - acc: 0.4953 - val_loss: 0.0500 - val_acc: 0.4486
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0175 - acc: 0.5035 - val_loss: 0.0502 - val_acc: 0.4019
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0168 - acc: 0.5117 - val_loss: 0.0470 - val_acc: 0.4299
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.0165 - acc: 0.5088 - val_loss: 0.0453 - val_acc: 0.4369
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0155 - acc: 0.5251 - val_loss: 0.0460 - val_acc: 0.4322
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0156 - acc: 0.5175 - val_loss: 0.0426 - val_acc: 0.4626
 *** training with sgd optimiser (unnormalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.1424 - acc: 0.7074 - val_loss: 0.1341 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.1214 - acc: 0.7074 - val_loss: 0.1127 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0994 - acc: 0.7074 - val_loss: 0.0888 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0731 - acc: 0.7074 - val_loss: 0.0580 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0394 - acc: 0.7074 - val_loss: 0.0223 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0130 - acc: 0.7074 - val_loss: 0.0077 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0067 - acc: 0.7074 - val_loss: 0.0059 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0059 - acc: 0.7074 - val_loss: 0.0056 - val_acc: 0.6963
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0058 - acc: 0.7074 - val_loss: 0.0055 - val_acc: 0.6963
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0057 - acc: 0.7074 - val_loss: 0.0055 - val_acc: 0.6963
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0056 - acc: 0.7074 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0056 - acc: 0.7074 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0056 - acc: 0.7074 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0055 - acc: 0.7074 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0055 - acc: 0.7074 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0055 - acc: 0.7074 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0055 - acc: 0.7074 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0055 - acc: 0.7074 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0054 - acc: 0.7074 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0054 - acc: 0.7074 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0054 - acc: 0.7074 - val_loss: 0.0052 - val_acc: 0.6963
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0054 - acc: 0.7074 - val_loss: 0.0052 - val_acc: 0.6963
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0054 - acc: 0.7074 - val_loss: 0.0052 - val_acc: 0.6963
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0053 - acc: 0.7074 - val_loss: 0.0052 - val_acc: 0.6963
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0053 - acc: 0.7074 - val_loss: 0.0052 - val_acc: 0.6963
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0053 - acc: 0.7074 - val_loss: 0.0052 - val_acc: 0.6963
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0053 - acc: 0.7074 - val_loss: 0.0051 - val_acc: 0.6963
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.0053 - acc: 0.7074 - val_loss: 0.0051 - val_acc: 0.6963
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0053 - acc: 0.7074 - val_loss: 0.0051 - val_acc: 0.6963
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0052 - acc: 0.7074 - val_loss: 0.0051 - val_acc: 0.6963
 *** training with rmsprop optimiser (normalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.1770 - acc: 0.4544 - val_loss: 0.0253 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.1083 - acc: 0.5607 - val_loss: 0.0068 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0798 - acc: 0.5993 - val_loss: 0.0108 - val_acc: 0.6939
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0936 - acc: 0.5660 - val_loss: 0.0058 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0703 - acc: 0.6046 - val_loss: 0.0078 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0653 - acc: 0.6157 - val_loss: 0.0048 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0777 - acc: 0.6151 - val_loss: 0.0117 - val_acc: 0.6939
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0758 - acc: 0.6197 - val_loss: 0.0087 - val_acc: 0.6939
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0602 - acc: 0.6379 - val_loss: 0.0376 - val_acc: 0.6449
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0772 - acc: 0.6273 - val_loss: 0.0462 - val_acc: 0.6565
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0657 - acc: 0.6285 - val_loss: 0.0508 - val_acc: 0.6449
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0604 - acc: 0.6238 - val_loss: 0.0394 - val_acc: 0.6612
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0540 - acc: 0.6303 - val_loss: 0.0443 - val_acc: 0.6589
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.1013 - acc: 0.4977 - val_loss: 0.0781 - val_acc: 0.6145
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0559 - acc: 0.6431 - val_loss: 0.0399 - val_acc: 0.6519
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0576 - acc: 0.6472 - val_loss: 0.0614 - val_acc: 0.6121
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0509 - acc: 0.6414 - val_loss: 0.0835 - val_acc: 0.6285
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0483 - acc: 0.6612 - val_loss: 0.0367 - val_acc: 0.6542
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0380 - acc: 0.6577 - val_loss: 0.0401 - val_acc: 0.6682
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0350 - acc: 0.6501 - val_loss: 0.0317 - val_acc: 0.6636
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0327 - acc: 0.5987 - val_loss: 0.0263 - val_acc: 0.6682
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0227 - acc: 0.6641 - val_loss: 0.0118 - val_acc: 0.6893
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0149 - acc: 0.6834 - val_loss: 0.0230 - val_acc: 0.6799
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0177 - acc: 0.6659 - val_loss: 0.0293 - val_acc: 0.6729
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0179 - acc: 0.6805 - val_loss: 0.0314 - val_acc: 0.6752
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0122 - acc: 0.6904 - val_loss: 0.0110 - val_acc: 0.6939
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0079 - acc: 0.6957 - val_loss: 0.0122 - val_acc: 0.6916
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.0101 - acc: 0.6922 - val_loss: 0.0093 - val_acc: 0.6939
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0071 - acc: 0.7004 - val_loss: 0.0164 - val_acc: 0.6939
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0070 - acc: 0.6986 - val_loss: 0.0104 - val_acc: 0.6939
 *** training with rmsprop optimiser (unnormalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.0317 - acc: 0.6530 - val_loss: 0.0055 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0067 - acc: 0.7074 - val_loss: 0.0056 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0062 - acc: 0.7074 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0054 - acc: 0.7074 - val_loss: 0.0057 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0051 - acc: 0.7074 - val_loss: 0.0107 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0048 - acc: 0.7068 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0043 - acc: 0.7068 - val_loss: 0.0035 - val_acc: 0.6986
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0035 - acc: 0.7056 - val_loss: 0.0031 - val_acc: 0.6986
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0027 - acc: 0.7220 - val_loss: 0.0022 - val_acc: 0.7266
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0022 - acc: 0.7424 - val_loss: 0.0027 - val_acc: 0.7173
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0019 - acc: 0.7453 - val_loss: 0.0023 - val_acc: 0.7523
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0016 - acc: 0.7757 - val_loss: 0.0017 - val_acc: 0.7266
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0014 - acc: 0.7833 - val_loss: 0.0019 - val_acc: 0.7523
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0013 - acc: 0.8131 - val_loss: 0.0014 - val_acc: 0.7874
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0011 - acc: 0.8148 - val_loss: 0.0026 - val_acc: 0.7710
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0011 - acc: 0.8265 - val_loss: 0.0020 - val_acc: 0.7897
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 9.6970e-04 - acc: 0.8359 - val_loss: 0.0020 - val_acc: 0.8154
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 9.3116e-04 - acc: 0.8452 - val_loss: 0.0015 - val_acc: 0.7967
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 8.4000e-04 - acc: 0.8633 - val_loss: 0.0019 - val_acc: 0.7547
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 7.7152e-04 - acc: 0.8610 - val_loss: 0.0021 - val_acc: 0.7967
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 7.6944e-04 - acc: 0.8621 - val_loss: 0.0016 - val_acc: 0.7897
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 6.9521e-04 - acc: 0.8797 - val_loss: 0.0014 - val_acc: 0.8131
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 6.8315e-04 - acc: 0.8797 - val_loss: 0.0014 - val_acc: 0.8037
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 6.2419e-04 - acc: 0.8861 - val_loss: 0.0012 - val_acc: 0.8107
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 6.0111e-04 - acc: 0.8890 - val_loss: 0.0014 - val_acc: 0.7967
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 5.8766e-04 - acc: 0.8832 - val_loss: 0.0014 - val_acc: 0.8037
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 5.0976e-04 - acc: 0.8954 - val_loss: 0.0016 - val_acc: 0.7897
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 5.4211e-04 - acc: 0.8972 - val_loss: 0.0012 - val_acc: 0.8107
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 4.8421e-04 - acc: 0.8989 - val_loss: 0.0013 - val_acc: 0.8178
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 4.8371e-04 - acc: 0.9089 - val_loss: 0.0014 - val_acc: 0.8131
 *** training with adagrad optimiser (normalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.2049 - acc: 0.4568 - val_loss: 0.0192 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0905 - acc: 0.5900 - val_loss: 0.0065 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0957 - acc: 0.6005 - val_loss: 0.0048 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0988 - acc: 0.6162 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0759 - acc: 0.6279 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0716 - acc: 0.6332 - val_loss: 0.0058 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0628 - acc: 0.6472 - val_loss: 0.0085 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0693 - acc: 0.6466 - val_loss: 0.0051 - val_acc: 0.6963
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0588 - acc: 0.6460 - val_loss: 0.0065 - val_acc: 0.6939
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0727 - acc: 0.6361 - val_loss: 0.0240 - val_acc: 0.6822
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0552 - acc: 0.6525 - val_loss: 0.0231 - val_acc: 0.6636
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0523 - acc: 0.6478 - val_loss: 0.0227 - val_acc: 0.6799
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0556 - acc: 0.6104 - val_loss: 0.0313 - val_acc: 0.6495
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0521 - acc: 0.6250 - val_loss: 0.0409 - val_acc: 0.6308
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0480 - acc: 0.6168 - val_loss: 0.0221 - val_acc: 0.6495
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0560 - acc: 0.5701 - val_loss: 0.0353 - val_acc: 0.6192
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0525 - acc: 0.5847 - val_loss: 0.0384 - val_acc: 0.6425
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0503 - acc: 0.6338 - val_loss: 0.0226 - val_acc: 0.6706
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0459 - acc: 0.6168 - val_loss: 0.0197 - val_acc: 0.6659
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0432 - acc: 0.6268 - val_loss: 0.0268 - val_acc: 0.6589
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0452 - acc: 0.6192 - val_loss: 0.0248 - val_acc: 0.6659
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0534 - acc: 0.6081 - val_loss: 0.0490 - val_acc: 0.6355
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0427 - acc: 0.6332 - val_loss: 0.0389 - val_acc: 0.6425
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0436 - acc: 0.6548 - val_loss: 0.0263 - val_acc: 0.6799
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0429 - acc: 0.6332 - val_loss: 0.0260 - val_acc: 0.6799
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0364 - acc: 0.6647 - val_loss: 0.0202 - val_acc: 0.6846
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0411 - acc: 0.6606 - val_loss: 0.0205 - val_acc: 0.6846
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.0373 - acc: 0.6478 - val_loss: 0.0364 - val_acc: 0.6659
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0471 - acc: 0.6402 - val_loss: 0.0349 - val_acc: 0.6706
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0389 - acc: 0.6560 - val_loss: 0.0293 - val_acc: 0.6402
 *** training with adagrad optimiser (unnormalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.4793 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.4856 - acc: 0.0000e+00 - val_loss: 0.4814 - val_acc: 0.0000e+00
 *** training with adadelta optimiser (normalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 2s - loss: 0.1837 - acc: 0.5701 - val_loss: 0.0641 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0814 - acc: 0.5929 - val_loss: 0.0192 - val_acc: 0.6846
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0731 - acc: 0.6139 - val_loss: 0.0096 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0692 - acc: 0.6051 - val_loss: 0.0050 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0605 - acc: 0.6121 - val_loss: 0.0050 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0708 - acc: 0.6057 - val_loss: 0.0061 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0514 - acc: 0.6268 - val_loss: 0.0055 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0521 - acc: 0.6379 - val_loss: 0.0051 - val_acc: 0.6963
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0462 - acc: 0.6495 - val_loss: 0.0054 - val_acc: 0.6869
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0470 - acc: 0.6197 - val_loss: 0.0348 - val_acc: 0.6565
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0466 - acc: 0.6489 - val_loss: 0.0114 - val_acc: 0.6916
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0444 - acc: 0.6425 - val_loss: 0.0089 - val_acc: 0.6846
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0412 - acc: 0.6384 - val_loss: 0.0144 - val_acc: 0.6799
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0403 - acc: 0.6636 - val_loss: 0.0270 - val_acc: 0.6893
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0424 - acc: 0.6828 - val_loss: 0.0120 - val_acc: 0.5374
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0399 - acc: 0.6308 - val_loss: 0.0919 - val_acc: 0.5280
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0368 - acc: 0.6425 - val_loss: 0.0552 - val_acc: 0.5818
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0355 - acc: 0.6525 - val_loss: 0.0434 - val_acc: 0.6706
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0324 - acc: 0.6817 - val_loss: 0.0795 - val_acc: 0.6262
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0378 - acc: 0.6478 - val_loss: 0.0741 - val_acc: 0.6192
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0324 - acc: 0.6612 - val_loss: 0.0788 - val_acc: 0.5093
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0311 - acc: 0.6776 - val_loss: 0.0354 - val_acc: 0.6355
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0331 - acc: 0.6758 - val_loss: 0.0829 - val_acc: 0.6075
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0319 - acc: 0.6741 - val_loss: 0.1375 - val_acc: 0.5421
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0283 - acc: 0.6600 - val_loss: 0.0180 - val_acc: 0.6636
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0326 - acc: 0.6653 - val_loss: 0.0544 - val_acc: 0.6425
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0304 - acc: 0.6805 - val_loss: 0.0520 - val_acc: 0.5935
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.0312 - acc: 0.6676 - val_loss: 0.0458 - val_acc: 0.6729
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0269 - acc: 0.6770 - val_loss: 0.0228 - val_acc: 0.6799
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0263 - acc: 0.6764 - val_loss: 0.0068 - val_acc: 0.6893
 *** training with adadelta optimiser (unnormalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.0227 - acc: 0.6565 - val_loss: 0.0152 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0083 - acc: 0.7074 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0058 - acc: 0.7074 - val_loss: 0.0048 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0050 - acc: 0.7074 - val_loss: 0.0048 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0049 - acc: 0.7074 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0047 - acc: 0.7074 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0046 - acc: 0.7074 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0047 - acc: 0.7074 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0044 - val_acc: 0.6963
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0049 - val_acc: 0.6963
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0044 - val_acc: 0.6963
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0044 - acc: 0.7074 - val_loss: 0.0050 - val_acc: 0.6963
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0044 - val_acc: 0.6963
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0044 - acc: 0.7074 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0044 - acc: 0.7074 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0044 - acc: 0.7074 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0044 - acc: 0.7074 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0043 - acc: 0.7074 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0043 - acc: 0.7074 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0042 - acc: 0.7074 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0043 - acc: 0.7074 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0042 - acc: 0.7074 - val_loss: 0.0041 - val_acc: 0.6963
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0041 - acc: 0.7074 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0041 - acc: 0.7074 - val_loss: 0.0050 - val_acc: 0.6963
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0040 - acc: 0.7074 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0039 - acc: 0.7074 - val_loss: 0.0042 - val_acc: 0.6963
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0039 - acc: 0.7074 - val_loss: 0.0038 - val_acc: 0.6963
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.0038 - acc: 0.7074 - val_loss: 0.0040 - val_acc: 0.6963
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0038 - acc: 0.7074 - val_loss: 0.0038 - val_acc: 0.6963
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0037 - acc: 0.7074 - val_loss: 0.0053 - val_acc: 0.6963
 *** training with adam optimiser (normalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 2s - loss: 0.2358 - acc: 0.4480 - val_loss: 0.0323 - val_acc: 0.6939
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0878 - acc: 0.6028 - val_loss: 0.0083 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0880 - acc: 0.6086 - val_loss: 0.0062 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0782 - acc: 0.6232 - val_loss: 0.0063 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0664 - acc: 0.6367 - val_loss: 0.0060 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0882 - acc: 0.5666 - val_loss: 0.0065 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0630 - acc: 0.5940 - val_loss: 0.0050 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0617 - acc: 0.6051 - val_loss: 0.0244 - val_acc: 0.6776
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0591 - acc: 0.6168 - val_loss: 0.0049 - val_acc: 0.6963
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0662 - acc: 0.5818 - val_loss: 0.0374 - val_acc: 0.6051
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0656 - acc: 0.5771 - val_loss: 0.0213 - val_acc: 0.6729
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0610 - acc: 0.6168 - val_loss: 0.0439 - val_acc: 0.6542
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0665 - acc: 0.5853 - val_loss: 0.0392 - val_acc: 0.6659
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0543 - acc: 0.6197 - val_loss: 0.0459 - val_acc: 0.6121
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0567 - acc: 0.5940 - val_loss: 0.0283 - val_acc: 0.6402
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0572 - acc: 0.6063 - val_loss: 0.0485 - val_acc: 0.6332
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0502 - acc: 0.6285 - val_loss: 0.0312 - val_acc: 0.6472
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0681 - acc: 0.5853 - val_loss: 0.0406 - val_acc: 0.6308
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0533 - acc: 0.6355 - val_loss: 0.0314 - val_acc: 0.6682
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0498 - acc: 0.6472 - val_loss: 0.0291 - val_acc: 0.6729
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0442 - acc: 0.6589 - val_loss: 0.0240 - val_acc: 0.6752
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0441 - acc: 0.6443 - val_loss: 0.0326 - val_acc: 0.6379
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0439 - acc: 0.6355 - val_loss: 0.0292 - val_acc: 0.6565
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0473 - acc: 0.6063 - val_loss: 0.0328 - val_acc: 0.6472
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0399 - acc: 0.6390 - val_loss: 0.0214 - val_acc: 0.6682
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0434 - acc: 0.6186 - val_loss: 0.0409 - val_acc: 0.6098
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0399 - acc: 0.6577 - val_loss: 0.0228 - val_acc: 0.6682
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.0307 - acc: 0.6641 - val_loss: 0.0211 - val_acc: 0.6589
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0275 - acc: 0.6653 - val_loss: 0.0174 - val_acc: 0.6752
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0210 - acc: 0.6770 - val_loss: 0.0186 - val_acc: 0.6752
 *** training with adam optimiser (unnormalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.0134 - acc: 0.6933 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0044 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0043 - acc: 0.7074 - val_loss: 0.0041 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0038 - acc: 0.7062 - val_loss: 0.0032 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0027 - acc: 0.7114 - val_loss: 0.0025 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0019 - acc: 0.7261 - val_loss: 0.0018 - val_acc: 0.7150
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0015 - acc: 0.7371 - val_loss: 0.0017 - val_acc: 0.7336
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0012 - acc: 0.7699 - val_loss: 0.0016 - val_acc: 0.7523
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0010 - acc: 0.7839 - val_loss: 0.0015 - val_acc: 0.7383
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 8.3365e-04 - acc: 0.8067 - val_loss: 0.0013 - val_acc: 0.7710
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 6.8362e-04 - acc: 0.8201 - val_loss: 0.0012 - val_acc: 0.7804
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 5.8436e-04 - acc: 0.8265 - val_loss: 0.0012 - val_acc: 0.7850
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 5.0863e-04 - acc: 0.8452 - val_loss: 0.0012 - val_acc: 0.7850
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 4.5286e-04 - acc: 0.8522 - val_loss: 0.0011 - val_acc: 0.8014
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 3.8902e-04 - acc: 0.8604 - val_loss: 0.0011 - val_acc: 0.7897
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 3.3695e-04 - acc: 0.8768 - val_loss: 0.0011 - val_acc: 0.8061
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 3.0681e-04 - acc: 0.8849 - val_loss: 0.0011 - val_acc: 0.8178
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 3.2753e-04 - acc: 0.8849 - val_loss: 0.0011 - val_acc: 0.7991
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 2.6346e-04 - acc: 0.8989 - val_loss: 0.0011 - val_acc: 0.8107
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 2.2713e-04 - acc: 0.9019 - val_loss: 0.0010 - val_acc: 0.8131
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 2.0261e-04 - acc: 0.9095 - val_loss: 0.0011 - val_acc: 0.7897
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 1.7614e-04 - acc: 0.9136 - val_loss: 0.0011 - val_acc: 0.8084
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 1.6391e-04 - acc: 0.9124 - val_loss: 0.0011 - val_acc: 0.8037
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 1.5148e-04 - acc: 0.9217 - val_loss: 0.0011 - val_acc: 0.7850
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 1.5547e-04 - acc: 0.9176 - val_loss: 0.0011 - val_acc: 0.8201
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 1.4659e-04 - acc: 0.9340 - val_loss: 0.0011 - val_acc: 0.8061
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 1.2562e-04 - acc: 0.9287 - val_loss: 0.0011 - val_acc: 0.8154
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 1.1497e-04 - acc: 0.9352 - val_loss: 0.0011 - val_acc: 0.8131
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 1.1082e-04 - acc: 0.9334 - val_loss: 0.0011 - val_acc: 0.8131
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 1.0626e-04 - acc: 0.9422 - val_loss: 0.0011 - val_acc: 0.8131
 *** training with adamax optimiser (normalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.2417 - acc: 0.2827 - val_loss: 0.0245 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0832 - acc: 0.5421 - val_loss: 0.0095 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0625 - acc: 0.5765 - val_loss: 0.0066 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0609 - acc: 0.6028 - val_loss: 0.0051 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0595 - acc: 0.5964 - val_loss: 0.0050 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0560 - acc: 0.6320 - val_loss: 0.0052 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0513 - acc: 0.6162 - val_loss: 0.0055 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0595 - acc: 0.6127 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0465 - acc: 0.6092 - val_loss: 0.0054 - val_acc: 0.6963
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0567 - acc: 0.5999 - val_loss: 0.0063 - val_acc: 0.6963
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0503 - acc: 0.6098 - val_loss: 0.0109 - val_acc: 0.6682
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0476 - acc: 0.6180 - val_loss: 0.1205 - val_acc: 0.5467
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0520 - acc: 0.6197 - val_loss: 0.0313 - val_acc: 0.6519
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0402 - acc: 0.6349 - val_loss: 0.0099 - val_acc: 0.6682
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0450 - acc: 0.6221 - val_loss: 0.0191 - val_acc: 0.6542
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0426 - acc: 0.6303 - val_loss: 0.0235 - val_acc: 0.6682
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0391 - acc: 0.6332 - val_loss: 0.0469 - val_acc: 0.6192
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0421 - acc: 0.6414 - val_loss: 0.0405 - val_acc: 0.6495
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0364 - acc: 0.6408 - val_loss: 0.0448 - val_acc: 0.6051
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0375 - acc: 0.6419 - val_loss: 0.0300 - val_acc: 0.6659
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0401 - acc: 0.6262 - val_loss: 0.0613 - val_acc: 0.5888
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0396 - acc: 0.6379 - val_loss: 0.1016 - val_acc: 0.5140
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0387 - acc: 0.6355 - val_loss: 0.0233 - val_acc: 0.6565
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0325 - acc: 0.6530 - val_loss: 0.0329 - val_acc: 0.6542
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0360 - acc: 0.6396 - val_loss: 0.0270 - val_acc: 0.6565
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0393 - acc: 0.6379 - val_loss: 0.0081 - val_acc: 0.6706
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0405 - acc: 0.6250 - val_loss: 0.0323 - val_acc: 0.6636
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 0.0368 - acc: 0.6460 - val_loss: 0.0524 - val_acc: 0.6028
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0346 - acc: 0.6478 - val_loss: 0.0246 - val_acc: 0.6636
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0312 - acc: 0.6373 - val_loss: 0.0150 - val_acc: 0.6729
 *** training with adamax optimiser (unnormalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.0194 - acc: 0.6945 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0044 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0044 - acc: 0.7074 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0043 - acc: 0.7074 - val_loss: 0.0044 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0042 - acc: 0.7074 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0040 - acc: 0.7074 - val_loss: 0.0037 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0033 - acc: 0.7062 - val_loss: 0.0030 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0025 - acc: 0.7114 - val_loss: 0.0023 - val_acc: 0.7103
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0020 - acc: 0.7220 - val_loss: 0.0019 - val_acc: 0.7126
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0017 - acc: 0.7319 - val_loss: 0.0018 - val_acc: 0.7150
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0014 - acc: 0.7453 - val_loss: 0.0016 - val_acc: 0.7220
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0013 - acc: 0.7593 - val_loss: 0.0015 - val_acc: 0.7336
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0011 - acc: 0.7722 - val_loss: 0.0013 - val_acc: 0.7664
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 9.4520e-04 - acc: 0.7926 - val_loss: 0.0013 - val_acc: 0.7734
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 8.7392e-04 - acc: 0.8049 - val_loss: 0.0012 - val_acc: 0.7827
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 7.6501e-04 - acc: 0.8125 - val_loss: 0.0012 - val_acc: 0.7921
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 6.8487e-04 - acc: 0.8218 - val_loss: 0.0012 - val_acc: 0.7850
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 5.9731e-04 - acc: 0.8341 - val_loss: 0.0012 - val_acc: 0.7944
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 5.5297e-04 - acc: 0.8306 - val_loss: 0.0011 - val_acc: 0.8037
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 5.1321e-04 - acc: 0.8388 - val_loss: 0.0011 - val_acc: 0.8061
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 4.5323e-04 - acc: 0.8423 - val_loss: 0.0011 - val_acc: 0.8014
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 4.1085e-04 - acc: 0.8487 - val_loss: 0.0011 - val_acc: 0.8084
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 3.6674e-04 - acc: 0.8627 - val_loss: 0.0011 - val_acc: 0.8061
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 3.4138e-04 - acc: 0.8586 - val_loss: 0.0011 - val_acc: 0.8154
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 3.1092e-04 - acc: 0.8703 - val_loss: 0.0011 - val_acc: 0.7897
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 2.9099e-04 - acc: 0.8768 - val_loss: 0.0011 - val_acc: 0.8084
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 2.8979e-04 - acc: 0.8814 - val_loss: 0.0010 - val_acc: 0.8178
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 2.6198e-04 - acc: 0.8808 - val_loss: 0.0010 - val_acc: 0.8037
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 2.4742e-04 - acc: 0.8908 - val_loss: 0.0011 - val_acc: 0.8084
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 2.1306e-04 - acc: 0.8937 - val_loss: 0.0010 - val_acc: 0.8154
 *** training with nadam optimiser (normalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 2s - loss: 0.1717 - acc: 0.4720 - val_loss: 0.0189 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.1025 - acc: 0.5426 - val_loss: 0.0088 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0916 - acc: 0.5526 - val_loss: 0.0066 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0825 - acc: 0.5783 - val_loss: 0.0051 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0708 - acc: 0.6011 - val_loss: 0.0107 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0735 - acc: 0.5637 - val_loss: 0.0053 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0571 - acc: 0.5888 - val_loss: 0.0055 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0568 - acc: 0.6192 - val_loss: 0.0142 - val_acc: 0.6776
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0603 - acc: 0.5917 - val_loss: 0.0207 - val_acc: 0.6729
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0623 - acc: 0.5981 - val_loss: 0.0252 - val_acc: 0.6682
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0564 - acc: 0.6086 - val_loss: 0.0573 - val_acc: 0.6215
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0453 - acc: 0.6373 - val_loss: 0.2300 - val_acc: 0.3972
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0812 - acc: 0.5835 - val_loss: 0.1990 - val_acc: 0.4019
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0584 - acc: 0.6133 - val_loss: 0.1337 - val_acc: 0.4953
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0615 - acc: 0.6174 - val_loss: 0.0668 - val_acc: 0.6028
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0481 - acc: 0.6343 - val_loss: 0.0613 - val_acc: 0.6005
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0475 - acc: 0.6343 - val_loss: 0.0300 - val_acc: 0.6519
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0332 - acc: 0.6379 - val_loss: 0.0248 - val_acc: 0.6729
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0249 - acc: 0.6682 - val_loss: 0.0177 - val_acc: 0.6729
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0174 - acc: 0.6787 - val_loss: 0.0066 - val_acc: 0.6939
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0109 - acc: 0.6863 - val_loss: 0.0083 - val_acc: 0.7009
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0069 - acc: 0.7027 - val_loss: 0.0053 - val_acc: 0.6939
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0047 - acc: 0.6986 - val_loss: 0.0085 - val_acc: 0.6986
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0042 - acc: 0.7062 - val_loss: 0.0069 - val_acc: 0.6963
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0040 - acc: 0.7062 - val_loss: 0.0058 - val_acc: 0.6986
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0038 - acc: 0.7231 - val_loss: 0.0056 - val_acc: 0.7150
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0034 - acc: 0.7208 - val_loss: 0.0059 - val_acc: 0.7150
Epoch 28/30
1712/1712 [==============================] - ETA: 0s - loss: 0.0032 - acc: 0.7241- ETA: 0s - loss: 0.0033 - acc: - 1s - loss: 0.0032 - acc: 0.7243 - val_loss: 0.0054 - val_acc: 0.7079
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 0.0030 - acc: 0.7313 - val_loss: 0.0053 - val_acc: 0.7009
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 0.0028 - acc: 0.7290 - val_loss: 0.0055 - val_acc: 0.7360
 *** training with nadam optimiser (unnormalised)***
Train on 1712 samples, validate on 428 samples
Epoch 1/30
1712/1712 [==============================] - 1s - loss: 0.0247 - acc: 0.6945 - val_loss: 0.0049 - val_acc: 0.6963
Epoch 2/30
1712/1712 [==============================] - 1s - loss: 0.0050 - acc: 0.7074 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 3/30
1712/1712 [==============================] - 1s - loss: 0.0047 - acc: 0.7074 - val_loss: 0.0044 - val_acc: 0.6963
Epoch 4/30
1712/1712 [==============================] - 1s - loss: 0.0047 - acc: 0.7074 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 5/30
1712/1712 [==============================] - 1s - loss: 0.0066 - acc: 0.7074 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 6/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 7/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 8/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 9/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0049 - val_acc: 0.6963
Epoch 10/30
1712/1712 [==============================] - 1s - loss: 0.0124 - acc: 0.7074 - val_loss: 0.0077 - val_acc: 0.6963
Epoch 11/30
1712/1712 [==============================] - 1s - loss: 0.0047 - acc: 0.7074 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 12/30
1712/1712 [==============================] - 1s - loss: 0.0046 - acc: 0.7074 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 13/30
1712/1712 [==============================] - 1s - loss: 0.0046 - acc: 0.7074 - val_loss: 0.0048 - val_acc: 0.6963
Epoch 14/30
1712/1712 [==============================] - 1s - loss: 0.0046 - acc: 0.7074 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 15/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 16/30
1712/1712 [==============================] - 1s - loss: 0.0045 - acc: 0.7074 - val_loss: 0.0046 - val_acc: 0.6963
Epoch 17/30
1712/1712 [==============================] - 1s - loss: 0.0044 - acc: 0.7074 - val_loss: 0.0043 - val_acc: 0.6963
Epoch 18/30
1712/1712 [==============================] - 1s - loss: 0.0042 - acc: 0.7074 - val_loss: 0.0044 - val_acc: 0.6916
Epoch 19/30
1712/1712 [==============================] - 1s - loss: 0.0040 - acc: 0.7056 - val_loss: 0.0038 - val_acc: 0.6869
Epoch 20/30
1712/1712 [==============================] - 1s - loss: 0.0031 - acc: 0.7033 - val_loss: 0.0031 - val_acc: 0.7009
Epoch 21/30
1712/1712 [==============================] - 1s - loss: 0.0025 - acc: 0.7225 - val_loss: 0.0024 - val_acc: 0.7196
Epoch 22/30
1712/1712 [==============================] - 1s - loss: 0.0022 - acc: 0.7331 - val_loss: 0.0028 - val_acc: 0.6986
Epoch 23/30
1712/1712 [==============================] - 1s - loss: 0.0018 - acc: 0.7383 - val_loss: 0.0018 - val_acc: 0.7266
Epoch 24/30
1712/1712 [==============================] - 1s - loss: 0.0015 - acc: 0.7523 - val_loss: 0.0019 - val_acc: 0.7243
Epoch 25/30
1712/1712 [==============================] - 1s - loss: 0.0013 - acc: 0.7617 - val_loss: 0.0019 - val_acc: 0.7313
Epoch 26/30
1712/1712 [==============================] - 1s - loss: 0.0012 - acc: 0.7664 - val_loss: 0.0017 - val_acc: 0.7313
Epoch 27/30
1712/1712 [==============================] - 1s - loss: 0.0010 - acc: 0.7850 - val_loss: 0.0017 - val_acc: 0.7360
Epoch 28/30
1712/1712 [==============================] - 1s - loss: 9.3574e-04 - acc: 0.7856 - val_loss: 0.0016 - val_acc: 0.7523
Epoch 29/30
1712/1712 [==============================] - 1s - loss: 8.2339e-04 - acc: 0.7886 - val_loss: 0.0015 - val_acc: 0.7570
Epoch 30/30
1712/1712 [==============================] - 1s - loss: 7.7625e-04 - acc: 0.8043 - val_loss: 0.0014 - val_acc: 0.7570

Leaving above evaluation cells.

After review, I will try and improve the brute force model I've used.

I will retain the rmsprop optimiser as it seems to have yielded decent results in the past.

In [77]:
def build_model_take_two():
    
    def add_conv(model, filters, **kwargs):
        model.add(Conv2D(filters=filters, kernel_size=3, padding='same', **kwargs))
        model.add(Activation('relu'))
    
    model = Sequential()
    add_conv(model, filters=32, input_shape=(96, 96, 1))
    add_conv(model, filters=32)
    model.add(MaxPooling2D(pool_size=4, padding='same'))
    add_conv(model, filters=128)
    add_conv(model, filters=128)
    model.add(MaxPooling2D(pool_size=4, padding='same'))
    add_conv(model, filters=512)
    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dense(30))

    return model

model = build_model()
In [78]:
## TODO: Compile the model
model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])

## TODO: Train the model
hist = model.fit(X_train, y_train, epochs=100, validation_split=0.2)

## TODO: Save the model as model.h5
model.save(f'take_two.h5')
Train on 1712 samples, validate on 428 samples
Epoch 1/100
1712/1712 [==============================] - 2s - loss: 0.1664 - acc: 0.4807 - val_loss: 0.0231 - val_acc: 0.6963
Epoch 2/100
1712/1712 [==============================] - 1s - loss: 0.1020 - acc: 0.5607 - val_loss: 0.0059 - val_acc: 0.6963
Epoch 3/100
1712/1712 [==============================] - 1s - loss: 0.0811 - acc: 0.5975 - val_loss: 0.0051 - val_acc: 0.6963
Epoch 4/100
1712/1712 [==============================] - 1s - loss: 0.0833 - acc: 0.5748 - val_loss: 0.0062 - val_acc: 0.6963
Epoch 5/100
1712/1712 [==============================] - 1s - loss: 0.0801 - acc: 0.6069 - val_loss: 0.0048 - val_acc: 0.6963
Epoch 6/100
1712/1712 [==============================] - 1s - loss: 0.0657 - acc: 0.5835 - val_loss: 0.0144 - val_acc: 0.6869
Epoch 7/100
1712/1712 [==============================] - 1s - loss: 0.0620 - acc: 0.6273 - val_loss: 0.0161 - val_acc: 0.6916
Epoch 8/100
1712/1712 [==============================] - 1s - loss: 0.0503 - acc: 0.6560 - val_loss: 0.0201 - val_acc: 0.6799
Epoch 9/100
1712/1712 [==============================] - 1s - loss: 0.0592 - acc: 0.6291 - val_loss: 0.0266 - val_acc: 0.6752
Epoch 10/100
1712/1712 [==============================] - 1s - loss: 0.0529 - acc: 0.6308 - val_loss: 0.0221 - val_acc: 0.6752
Epoch 11/100
1712/1712 [==============================] - 1s - loss: 0.0492 - acc: 0.6449 - val_loss: 0.0216 - val_acc: 0.6776
Epoch 12/100
1712/1712 [==============================] - 1s - loss: 0.0597 - acc: 0.6361 - val_loss: 0.0451 - val_acc: 0.6822
Epoch 13/100
1712/1712 [==============================] - 1s - loss: 0.0443 - acc: 0.6589 - val_loss: 0.0613 - val_acc: 0.6262
Epoch 14/100
1712/1712 [==============================] - 1s - loss: 0.0468 - acc: 0.6595 - val_loss: 0.0555 - val_acc: 0.6402
Epoch 15/100
1712/1712 [==============================] - 1s - loss: 0.0372 - acc: 0.6659 - val_loss: 0.0370 - val_acc: 0.6425
Epoch 16/100
1712/1712 [==============================] - 1s - loss: 0.0426 - acc: 0.6431 - val_loss: 0.0336 - val_acc: 0.6589
Epoch 17/100
1712/1712 [==============================] - 1s - loss: 0.0388 - acc: 0.6443 - val_loss: 0.0348 - val_acc: 0.6659
Epoch 18/100
1712/1712 [==============================] - 1s - loss: 0.0333 - acc: 0.6606 - val_loss: 0.0301 - val_acc: 0.6729
Epoch 19/100
1712/1712 [==============================] - 1s - loss: 0.0252 - acc: 0.6466 - val_loss: 0.0178 - val_acc: 0.6682
Epoch 20/100
1712/1712 [==============================] - 1s - loss: 0.0217 - acc: 0.6741 - val_loss: 0.0155 - val_acc: 0.6682
Epoch 21/100
1712/1712 [==============================] - 1s - loss: 0.0164 - acc: 0.6752 - val_loss: 0.0056 - val_acc: 0.6963
Epoch 22/100
1712/1712 [==============================] - 1s - loss: 0.0112 - acc: 0.6793 - val_loss: 0.0111 - val_acc: 0.6822
Epoch 23/100
1712/1712 [==============================] - 1s - loss: 0.0087 - acc: 0.6787 - val_loss: 0.0063 - val_acc: 0.6916
Epoch 24/100
1712/1712 [==============================] - 1s - loss: 0.0064 - acc: 0.6928 - val_loss: 0.0056 - val_acc: 0.6916
Epoch 25/100
1712/1712 [==============================] - 1s - loss: 0.0057 - acc: 0.6992 - val_loss: 0.0141 - val_acc: 0.6729
Epoch 26/100
1712/1712 [==============================] - 1s - loss: 0.0048 - acc: 0.7068 - val_loss: 0.0047 - val_acc: 0.6963
Epoch 27/100
1712/1712 [==============================] - 1s - loss: 0.0047 - acc: 0.7027 - val_loss: 0.0045 - val_acc: 0.6963
Epoch 28/100
1712/1712 [==============================] - 1s - loss: 0.0039 - acc: 0.7079 - val_loss: 0.0063 - val_acc: 0.6986
Epoch 29/100
1712/1712 [==============================] - 1s - loss: 0.0037 - acc: 0.7114 - val_loss: 0.0064 - val_acc: 0.6939
Epoch 30/100
1712/1712 [==============================] - 1s - loss: 0.0032 - acc: 0.7220 - val_loss: 0.0034 - val_acc: 0.6916
Epoch 31/100
1712/1712 [==============================] - 1s - loss: 0.0042 - acc: 0.7231 - val_loss: 0.0075 - val_acc: 0.6916
Epoch 32/100
1712/1712 [==============================] - 1s - loss: 0.0033 - acc: 0.7109 - val_loss: 0.0051 - val_acc: 0.6986
Epoch 33/100
1712/1712 [==============================] - 1s - loss: 0.0027 - acc: 0.7383 - val_loss: 0.0029 - val_acc: 0.6963
Epoch 34/100
1712/1712 [==============================] - 1s - loss: 0.0026 - acc: 0.7290 - val_loss: 0.0032 - val_acc: 0.6939
Epoch 35/100
1712/1712 [==============================] - 1s - loss: 0.0024 - acc: 0.7301 - val_loss: 0.0025 - val_acc: 0.7103
Epoch 36/100
1712/1712 [==============================] - 1s - loss: 0.0021 - acc: 0.7465 - val_loss: 0.0027 - val_acc: 0.7033
Epoch 37/100
1712/1712 [==============================] - 1s - loss: 0.0020 - acc: 0.7424 - val_loss: 0.0023 - val_acc: 0.7313
Epoch 38/100
1712/1712 [==============================] - 1s - loss: 0.0019 - acc: 0.7377 - val_loss: 0.0020 - val_acc: 0.7290
Epoch 39/100
1712/1712 [==============================] - 1s - loss: 0.0018 - acc: 0.7488 - val_loss: 0.0022 - val_acc: 0.7220
Epoch 40/100
1712/1712 [==============================] - 1s - loss: 0.0017 - acc: 0.7465 - val_loss: 0.0022 - val_acc: 0.7266
Epoch 41/100
1712/1712 [==============================] - 1s - loss: 0.0017 - acc: 0.7506 - val_loss: 0.0026 - val_acc: 0.7196
Epoch 42/100
1712/1712 [==============================] - 1s - loss: 0.0023 - acc: 0.7588 - val_loss: 0.0018 - val_acc: 0.7220
Epoch 43/100
1712/1712 [==============================] - 1s - loss: 0.0029 - acc: 0.7699 - val_loss: 0.0106 - val_acc: 0.4930
Epoch 44/100
1712/1712 [==============================] - 1s - loss: 0.0016 - acc: 0.7477 - val_loss: 0.0045 - val_acc: 0.7009
Epoch 45/100
1712/1712 [==============================] - 1s - loss: 0.0014 - acc: 0.7634 - val_loss: 0.0023 - val_acc: 0.7196
Epoch 46/100
1712/1712 [==============================] - 1s - loss: 0.0014 - acc: 0.7745 - val_loss: 0.0015 - val_acc: 0.7453
Epoch 47/100
1712/1712 [==============================] - 1s - loss: 0.0013 - acc: 0.7810 - val_loss: 0.0016 - val_acc: 0.7430
Epoch 48/100
1712/1712 [==============================] - 1s - loss: 0.0012 - acc: 0.7739 - val_loss: 0.0022 - val_acc: 0.7477
Epoch 49/100
1712/1712 [==============================] - 1s - loss: 0.0013 - acc: 0.7961 - val_loss: 0.0015 - val_acc: 0.7640
Epoch 50/100
1712/1712 [==============================] - 1s - loss: 0.0023 - acc: 0.7804 - val_loss: 0.0018 - val_acc: 0.7570
Epoch 51/100
1712/1712 [==============================] - 1s - loss: 0.0011 - acc: 0.8107 - val_loss: 0.0019 - val_acc: 0.7313
Epoch 52/100
1712/1712 [==============================] - 1s - loss: 0.0011 - acc: 0.8026 - val_loss: 0.0016 - val_acc: 0.7640
Epoch 53/100
1712/1712 [==============================] - 1s - loss: 0.0011 - acc: 0.7985 - val_loss: 0.0018 - val_acc: 0.7593
Epoch 54/100
1712/1712 [==============================] - 1s - loss: 0.0011 - acc: 0.8154 - val_loss: 0.0022 - val_acc: 0.7547
Epoch 55/100
1712/1712 [==============================] - 1s - loss: 0.0010 - acc: 0.7985 - val_loss: 0.0021 - val_acc: 0.7290
Epoch 56/100
1712/1712 [==============================] - 1s - loss: 9.8770e-04 - acc: 0.8119 - val_loss: 0.0029 - val_acc: 0.7056
Epoch 57/100
1712/1712 [==============================] - 1s - loss: 9.6907e-04 - acc: 0.7985 - val_loss: 0.0017 - val_acc: 0.7921
Epoch 58/100
1712/1712 [==============================] - 1s - loss: 9.4964e-04 - acc: 0.8055 - val_loss: 0.0021 - val_acc: 0.7547
Epoch 59/100
1712/1712 [==============================] - 1s - loss: 9.4826e-04 - acc: 0.8172 - val_loss: 0.0018 - val_acc: 0.7640
Epoch 60/100
1712/1712 [==============================] - 1s - loss: 8.1635e-04 - acc: 0.8306 - val_loss: 0.0016 - val_acc: 0.7500
Epoch 61/100
1712/1712 [==============================] - 1s - loss: 8.6932e-04 - acc: 0.8154 - val_loss: 0.0015 - val_acc: 0.7640
Epoch 62/100
1712/1712 [==============================] - 1s - loss: 8.2747e-04 - acc: 0.8347 - val_loss: 0.0019 - val_acc: 0.7780
Epoch 63/100
1712/1712 [==============================] - 1s - loss: 8.4599e-04 - acc: 0.8224 - val_loss: 0.0013 - val_acc: 0.7921
Epoch 64/100
1712/1712 [==============================] - 1s - loss: 7.9143e-04 - acc: 0.8359 - val_loss: 0.0018 - val_acc: 0.7313
Epoch 65/100
1712/1712 [==============================] - 1s - loss: 7.7869e-04 - acc: 0.8300 - val_loss: 0.0013 - val_acc: 0.7757
Epoch 66/100
1712/1712 [==============================] - 1s - loss: 8.0861e-04 - acc: 0.8458 - val_loss: 0.0016 - val_acc: 0.7921
Epoch 67/100
1712/1712 [==============================] - 1s - loss: 6.9517e-04 - acc: 0.8341 - val_loss: 0.0015 - val_acc: 0.7850
Epoch 68/100
1712/1712 [==============================] - 1s - loss: 7.4993e-04 - acc: 0.8376 - val_loss: 0.0013 - val_acc: 0.7617
Epoch 69/100
1712/1712 [==============================] - 1s - loss: 7.5401e-04 - acc: 0.8271 - val_loss: 0.0014 - val_acc: 0.7780
Epoch 70/100
1712/1712 [==============================] - 1s - loss: 7.3735e-04 - acc: 0.8400 - val_loss: 0.0013 - val_acc: 0.7687
Epoch 71/100
1712/1712 [==============================] - 1s - loss: 6.9859e-04 - acc: 0.8435 - val_loss: 0.0016 - val_acc: 0.7757
Epoch 72/100
1712/1712 [==============================] - 1s - loss: 6.5540e-04 - acc: 0.8528 - val_loss: 0.0030 - val_acc: 0.7360
Epoch 73/100
1712/1712 [==============================] - 1s - loss: 6.4285e-04 - acc: 0.8458 - val_loss: 0.0014 - val_acc: 0.7687
Epoch 74/100
1712/1712 [==============================] - 1s - loss: 6.4440e-04 - acc: 0.8516 - val_loss: 0.0012 - val_acc: 0.7780
Epoch 75/100
1712/1712 [==============================] - 1s - loss: 6.0292e-04 - acc: 0.8621 - val_loss: 0.0016 - val_acc: 0.7804
Epoch 76/100
1712/1712 [==============================] - 1s - loss: 5.9635e-04 - acc: 0.8505 - val_loss: 0.0014 - val_acc: 0.7944
Epoch 77/100
1712/1712 [==============================] - 1s - loss: 5.5986e-04 - acc: 0.8621 - val_loss: 0.0015 - val_acc: 0.7617
Epoch 78/100
1712/1712 [==============================] - 1s - loss: 5.7527e-04 - acc: 0.8610 - val_loss: 0.0014 - val_acc: 0.8014
Epoch 79/100
1712/1712 [==============================] - 1s - loss: 5.4594e-04 - acc: 0.8604 - val_loss: 0.0014 - val_acc: 0.7804
Epoch 80/100
1712/1712 [==============================] - 1s - loss: 6.1282e-04 - acc: 0.8557 - val_loss: 0.0013 - val_acc: 0.7710
Epoch 81/100
1712/1712 [==============================] - 1s - loss: 5.4360e-04 - acc: 0.8610 - val_loss: 0.0016 - val_acc: 0.7827
Epoch 82/100
1712/1712 [==============================] - 1s - loss: 4.9896e-04 - acc: 0.8657 - val_loss: 0.0014 - val_acc: 0.7804
Epoch 83/100
1712/1712 [==============================] - 1s - loss: 0.0011 - acc: 0.8522 - val_loss: 0.0013 - val_acc: 0.7897
Epoch 84/100
1712/1712 [==============================] - 1s - loss: 4.6543e-04 - acc: 0.8703 - val_loss: 0.0013 - val_acc: 0.7874
Epoch 85/100
1712/1712 [==============================] - 1s - loss: 4.9353e-04 - acc: 0.8715 - val_loss: 0.0017 - val_acc: 0.7664
Epoch 86/100
1712/1712 [==============================] - 1s - loss: 4.9601e-04 - acc: 0.8756 - val_loss: 0.0020 - val_acc: 0.7944
Epoch 87/100
1712/1712 [==============================] - 1s - loss: 5.0041e-04 - acc: 0.8762 - val_loss: 0.0014 - val_acc: 0.7734
Epoch 88/100
1712/1712 [==============================] - 1s - loss: 5.2183e-04 - acc: 0.8931 - val_loss: 0.0015 - val_acc: 0.7944
Epoch 89/100
1712/1712 [==============================] - 1s - loss: 5.3293e-04 - acc: 0.8727 - val_loss: 0.0012 - val_acc: 0.7874
Epoch 90/100
1712/1712 [==============================] - 1s - loss: 4.2412e-04 - acc: 0.8785 - val_loss: 0.0013 - val_acc: 0.7827
Epoch 91/100
1712/1712 [==============================] - 1s - loss: 4.7537e-04 - acc: 0.8785 - val_loss: 0.0012 - val_acc: 0.7734
Epoch 92/100
1712/1712 [==============================] - 1s - loss: 4.3425e-04 - acc: 0.8838 - val_loss: 0.0012 - val_acc: 0.7897
Epoch 93/100
1712/1712 [==============================] - 1s - loss: 4.5091e-04 - acc: 0.8820 - val_loss: 0.0017 - val_acc: 0.7664
Epoch 94/100
1712/1712 [==============================] - 1s - loss: 4.2398e-04 - acc: 0.8826 - val_loss: 0.0012 - val_acc: 0.7991
Epoch 95/100
1712/1712 [==============================] - 1s - loss: 4.2429e-04 - acc: 0.8814 - val_loss: 0.0011 - val_acc: 0.8014
Epoch 96/100
1712/1712 [==============================] - 1s - loss: 3.9999e-04 - acc: 0.8814 - val_loss: 0.0012 - val_acc: 0.8014
Epoch 97/100
1712/1712 [==============================] - 1s - loss: 3.9725e-04 - acc: 0.8861 - val_loss: 0.0011 - val_acc: 0.7827
Epoch 98/100
1712/1712 [==============================] - 1s - loss: 4.2600e-04 - acc: 0.8803 - val_loss: 0.0012 - val_acc: 0.7804
Epoch 99/100
1712/1712 [==============================] - 1s - loss: 4.0217e-04 - acc: 0.8855 - val_loss: 0.0012 - val_acc: 0.7874
Epoch 100/100
1712/1712 [==============================] - 1s - loss: 3.8589e-04 - acc: 0.8966 - val_loss: 0.0018 - val_acc: 0.7664

Step 7: Visualize the Loss and Test Predictions

(IMPLEMENTATION) Answer a few questions and visualize the loss

Question 1: Outline the steps you took to get to your final neural network architecture and your reasoning at each step.

Answer:

I believe I went for overkill brute force in my architecture: 2D convolution layers, each reducing the dimensionality by a factor of 2 and doubling the number of filters.

I tried to see if adding normalisation after each convolution would improve the training.

I initially tried stopped with 6x6 convolution layer and 512 filters as I thought it would not pick many relevant features if they were 3x3, but it turned out adding a 3x3 convolution layer with 1024 filters improve slightly the accuracy and its curve over 30 epochs with the best performing optimisers (implying test accuracy may actually improve further if I increased the number of epochs as opposed to declining)

Question 2: Defend your choice of optimizer. Which optimizers did you test, and how did you determine which worked best?

Answer:

I didn't choose an optimiser. The optimisers competed for the selection. I will pick the one that produces the lowest validation loss after 30 epochs.

Use the code cell below to plot the training and validation loss of your neural network. You may find this resource useful.

In [10]:
print(hists["unnormalised"]["rmsprop"].history.keys())
dict_keys(['val_loss', 'val_acc', 'loss', 'acc'])
In [5]:
## TODO: Visualize the training and validation loss of your neural network

def draw_plot(ax_loss, ax_acc):
    ax_loss.plot(history.history['loss'])
    ax_loss.plot(history.history['val_loss'])

    ax_acc.plot(history.history['acc'])
    ax_acc.plot(history.history['val_acc'])
    ax_loss.set(
        title = f'{normalised} {optimiser}',
        xlabel = 'epoch', ylabel = 'loss',
    )
    ax_acc.set(
        title = f'{normalised} {optimiser}',
        xlabel = 'epoch', ylabel = 'accuracy',
    )
    ax_loss.legend(['train', 'test'], loc='upper left')
    ax_acc.legend(['train', 'test'], loc='upper left')
    
fig, all_subs = plt.subplots(nrows=14, ncols=2, sharex=True)

fig.set_size_inches((10, 40))

all_subs = iter(all_subs)

for normalised, histories in hists.items():
    for optimiser, history in histories.items():
        draw_plot(*next(all_subs))
plt.show()

Now let's test the simplified network. This is been trained on a lot more epochs (100):

In [79]:
def draw_plot(ax_loss, ax_acc):
    ax_loss.plot(history.history['loss'])
    ax_loss.plot(history.history['val_loss'])

    ax_acc.plot(history.history['acc'])
    ax_acc.plot(history.history['val_acc'])
    ax_loss.set(
        title = f'{normalised} {optimiser}',
        xlabel = 'epoch', ylabel = 'loss',
    )
    ax_acc.set(
        title = f'{normalised} {optimiser}',
        xlabel = 'epoch', ylabel = 'accuracy',
    )
    ax_loss.legend(['train', 'test'], loc='upper left')
    ax_acc.legend(['train', 'test'], loc='upper left')
    
fig, (ax_loss, ax_acc) = plt.subplots(nrows=1, ncols=2, sharex=True)

fig.set_size_inches((40, 10))

ax_loss.plot(hist.history['loss'])
ax_loss.plot(hist.history['val_loss'])

ax_acc.plot(hist.history['acc'])
ax_acc.plot(hist.history['val_acc'])
ax_loss.set(
    xlabel = 'epoch', ylabel = 'loss',
)
ax_acc.set(
    xlabel = 'epoch', ylabel = 'accuracy',
)
ax_loss.legend(['train', 'test'], loc='upper left')
ax_acc.legend(['train', 'test'], loc='upper left')

plt.show()

Question 3: Do you notice any evidence of overfitting or underfitting in the above plot? If so, what steps have you taken to improve your model? Note that slight overfitting or underfitting will not hurt your chances of a successful submission, as long as you have attempted some solutions towards improving your model (such as regularization, dropout, increased/decreased number of layers, etc).

Answer:

Now we can sit down and examine the different combinations. The normalisation is definitely getting in the way, at least if applied after each convolutional layer.

On the other hand in terms of accuracy and loss, the unnormalised model seems to be faring better.

However, there are a few things we can notice:

  • sgd and adadelta, nadam are plateauing and are going nowhere both in terms of accuracy or loss.
  • adagrad fails to converge to a solution altogether. I suspect it requires different metaparameters.
  • adam and adamax show signs of overfitting on epoch 7 as training loss and accuracy clearly overtake validation
  • rmsprop also show signs of overfitting, however the accuracy appears like it would show some progress beyond 30 epochs

For 30 epochs, however, adam appears to yield the best results for that model and will be the one I will use the results of.

I originally had less convolutional layers, but they yielded worse loss and accuracy. I therefore added a last 1024 filters convolutional layer at the end.

I'm not sure how to improve that model and whether adding pooling layers and/or dense layers with normalisation in the middle would improve the solution. It would appear, however that the results are decent if ran on samples.

AFTER PROJECT REVIEW

Once the model simplified, we can see that, by pushing the number of epochs to 100, we can get a marginally better accuracy, hovering towards 0.8. The model is definitely overfitting however as the training accuracy is eventually diverging from the validation score.

Visualize a Subset of the Test Predictions

Execute the code cell below to visualize your model's predicted keypoints on a subset of the testing images.

In [80]:
y_test = model.predict(X_test)
fig = plt.figure(figsize=(20,20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1, xticks=[], yticks=[])
    plot_data(X_test[i], y_test[i], ax)

plt.show()

Step 8: Complete the pipeline

With the work you did in Sections 1 and 2 of this notebook, along with your freshly trained facial keypoint detector, you can now complete the full pipeline. That is given a color image containing a person or persons you can now

  • Detect the faces in this image automatically using OpenCV
  • Predict the facial keypoints in each face detected in the image
  • Paint predicted keypoints on each face detected

In this Subsection you will do just this!

(IMPLEMENTATION) Facial Keypoints Detector

Use the OpenCV face detection functionality you built in previous Sections to expand the functionality of your keypoints detector to color images with arbitrary size. Your function should perform the following steps

  1. Accept a color image.
  2. Convert the image to grayscale.
  3. Detect and crop the face contained in the image.
  4. Locate the facial keypoints in the cropped image.
  5. Overlay the facial keypoints in the original (color, uncropped) image.

Note: step 4 can be the trickiest because remember your convolutional network is only trained to detect facial keypoints in $96 \times 96$ grayscale images where each pixel was normalized to lie in the interval $[0,1]$, and remember that each facial keypoint was normalized during training to the interval $[-1,1]$. This means - practically speaking - to paint detected keypoints onto a test face you need to perform this same pre-processing to your candidate face - that is after detecting it you should resize it to $96 \times 96$ and normalize its values before feeding it into your facial keypoint detector. To be shown correctly on the original image the output keypoints from your detector then need to be shifted and re-normalized from the interval $[-1,1]$ to the width and height of your detected face.

When complete you should be able to produce example images like the one below

In [41]:
# Load in color image for face detection
image = cv2.imread('images/obamas4.jpg')


# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


# plot our image
fig = plt.figure(figsize = (9,9))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('image copy')
ax1.imshow(image)
Out[41]:
<matplotlib.image.AxesImage at 0x7f4a87985ef0>
In [8]:
image.shape
Out[8]:
(500, 759, 3)
In [81]:
from keras.models import load_model

model = load_model('take_two.h5')

def key_points(grey, result):

    X = np.array([(cv2.resize(grey, (96, 96)) / 255).reshape(96,96,1)], np.float32)
    
    y = model.predict(X)
    key_points = ((y + 1)/2)[0].reshape(-1, 2)
    
    return [
        (int(x * result.shape[0]), int(y * result.shape[1]))
        for x, y in key_points
    ]

def process_faces(source, action):
    result = np.copy(source) 
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    
    face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
    for (x,y,w,h) in face_cascade.detectMultiScale(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)):
        action(gray[y:y+h, x:x+w], result[y:y+h, x:x+w])
        
    return result
In [82]:
def render_keypoints(grey, result):
    for x,y in key_points(grey, result):
        cv2.circle(result, (x,y), 3, (0,255,0), -1)

display_image(
    process_faces(image, render_keypoints), 
    title='detected facial key points'
)

(Optional) Further Directions - add a filter using facial keypoints to your laptop camera

Now you can add facial keypoint detection to your laptop camera - as illustrated in the gif below.

The next Python cell contains the basic laptop video camera function used in the previous optional video exercises. Combine it with the functionality you developed for keypoint detection and marking in the previous exercise and you should be good to go!

In [26]:
import cv2
import time 
from keras.models import load_model
def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # keep video stream open
    while rval:
        # plot image from camera with detections marked
        cv2.imshow("face detection activated", process_faces(frame, render_keypoints))
        
        # exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # exit by pressing any key
            # destroy windows
            cv2.destroyAllWindows()
            
            # hack from stack overflow for making sure window closes on osx --> https://stackoverflow.com/questions/6116564/destroywindow-does-not-close-window-on-mac-using-python-and-opencv
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()  
In [27]:
# Run your keypoint face painter
laptop_camera_go()
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-27-503658b0992c> in <module>()
      1 # Run your keypoint face painter
----> 2 laptop_camera_go()

<ipython-input-26-b6fd1ea0d10f> in laptop_camera_go()
      4 def laptop_camera_go():
      5     # Create instance of video capturer
----> 6     cv2.namedWindow("face detection activated")
      7     vc = cv2.VideoCapture(0)
      8 

error: /io/opencv/modules/highgui/src/window.cpp:565: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow

(Optional) Further Directions - add a filter using facial keypoints

Using your freshly minted facial keypoint detector pipeline you can now do things like add fun filters to a person's face automatically. In this optional exercise you can play around with adding sunglasses automatically to each individual's face in an image as shown in a demonstration image below.

To produce this effect an image of a pair of sunglasses shown in the Python cell below.

In [8]:
# Load in sunglasses image - note the usage of the special option
# cv2.IMREAD_UNCHANGED, this option is used because the sunglasses 
# image has a 4th channel that allows us to control how transparent each pixel in the image is
sunglasses = cv2.imread("images/sunglasses_4.png", cv2.IMREAD_UNCHANGED)

# Plot the image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.imshow(sunglasses)
ax1.axis('off');

This image is placed over each individual's face using the detected eye points to determine the location of the sunglasses, and eyebrow points to determine the size that the sunglasses should be for each person (one could also use the nose point to determine this).

Notice that this image actually has 4 channels, not just 3.

In [9]:
# Print out the shape of the sunglasses image
print ('The sunglasses image has shape: ' + str(np.shape(sunglasses)))
The sunglasses image has shape: (1123, 3064, 4)

It has the usual red, blue, and green channels any color image has, with the 4th channel representing the transparency level of each pixel in the image. Here's how the transparency channel works: the lower the value, the more transparent the pixel will become. The lower bound (completely transparent) is zero here, so any pixels set to 0 will not be seen.

This is how we can place this image of sunglasses on someone's face and still see the area around of their face where the sunglasses lie - because these pixels in the sunglasses image have been made completely transparent.

Lets check out the alpha channel of our sunglasses image in the next Python cell. Note because many of the pixels near the boundary are transparent we'll need to explicitly print out non-zero values if we want to see them.

In [10]:
# Print out the sunglasses transparency (alpha) channel
alpha_channel = sunglasses[:,:,3]
print ('the alpha channel here looks like')
print (alpha_channel)

# Just to double check that there are indeed non-zero values
# Let's find and print out every value greater than zero
values = np.where(alpha_channel != 0)
print ('\n the non-zero values of the alpha channel look like')
print (values)
the alpha channel here looks like
[[0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 ..., 
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]]

 the non-zero values of the alpha channel look like
(array([  17,   17,   17, ..., 1109, 1109, 1109]), array([ 687,  688,  689, ..., 2376, 2377, 2378]))

This means that when we place this sunglasses image on top of another image, we can use the transparency channel as a filter to tell us which pixels to overlay on a new image (only the non-transparent ones with values greater than zero).

One last thing: it's helpful to understand which keypoint belongs to the eyes, mouth, etc. So, in the image below, we also display the index of each facial keypoint directly on the image so that you can tell which keypoints are for the eyes, eyebrows, etc.

With this information, you're well on your way to completing this filtering task! See if you can place the sunglasses automatically on the individuals in the image loaded in / shown in the next Python cell.

In [31]:
# Load in color image for face detection
image = cv2.imread('images/obamas4.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


# Plot the image
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('Original Image')
ax1.imshow(image)
Out[31]:
<matplotlib.image.AxesImage at 0x7f7ed7acf9e8>
In [17]:
# I attempted it, but it's taking a bit too long to get working. 
# 2 blitting isn't the most interesting problem to solve.

def glass_up_face(grey, result):
    
    print(grey.shape, result.shape)
    all_key_points = key_points(grey, result)
    
    right_eye, left_eye = np.array(all_key_points)[:2]
    
    dist_x = right_eye[0] - left_eye[0]
    
    scale = dist_x / (sunglasses.shape[0]/2)  
    
    scaled_sunglasses = cv2.resize(sunglasses,None,fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
    
    scaled_shape = scaled_sunglasses.shape
    print(scaled_shape)
    
    centre = (left_eye + right_eye) / 2
    top_left = centre - np.array([scaled_shape[0], scaled_shape[1]])/2
    
    for x in range(scaled_shape[0]):
        for y in range(scaled_shape[1]):
            r, g, b, a = scaled_sunglasses[x, y]
            if a:

                result[x + int(top_left[0])][y + int(top_left[1])] = [r, g, b]

display_image(
    process_faces(image, glass_up_face), 
    title='faces with glasses'
)
(165, 165) (165, 165, 3)
(128, 349, 4)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-17-b5035e5f3ba4> in <module>()
     26 
     27 display_image(
---> 28     process_faces(image, glass_up_face),
     29     title='faces with glasses'
     30 )

<ipython-input-6-6082e8b9c265> in process_faces(source, action)
     21     face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
     22     for (x,y,w,h) in face_cascade.detectMultiScale(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)):
---> 23         action(gray[y:y+h, x:x+w], result[y:y+h, x:x+w])
     24 
     25     return result

<ipython-input-17-b5035e5f3ba4> in glass_up_face(grey, result)
     23             if a:
     24 
---> 25                 result[x + int(top_left[0])][y + int(top_left[1])] = [r, g, b]
     26 
     27 display_image(

IndexError: index 165 is out of bounds for axis 0 with size 165

(Optional) Further Directions - add a filter using facial keypoints to your laptop camera

Now you can add the sunglasses filter to your laptop camera - as illustrated in the gif below.

The next Python cell contains the basic laptop video camera function used in the previous optional video exercises. Combine it with the functionality you developed for adding sunglasses to someone's face in the previous optional exercise and you should be good to go!

In [ ]:
import cv2
import time 
from keras.models import load_model
import numpy as np

def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep video stream open
    while rval:
        # Plot image from camera with detections marked
        cv2.imshow("face detection activated", frame)
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # exit by pressing any key
            # Destroy windows 
            cv2.destroyAllWindows()
            
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
        
In [ ]:
# Load facial landmark detector model
model = load_model('my_model.h5')

# Run sunglasses painter
laptop_camera_go()